package com.fjec.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjec.base.entity.SysDict;
import com.fjec.base.mapper.SysDictMapper;
import com.fjec.base.service.ISysDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjec.base.view.ListItem;
import com.fjec.common.util.DateUtil;
import com.fjec.common.util.FileUtil;
import com.fjec.common.util.JsonUtil;
import com.fjec.common.util.SequenceIdGenerator;
import org.apache.commons.collections.MapUtils;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 003.字典表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-08-31
 */
@Service
@Transactional
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements ISysDictService {

    @Resource
    private SysDictMapper mapper;

    @Resource
    private SequenceIdGenerator idGenerator;

    @Override
    @Cacheable(value = "dictCache", key = "'fjec:dict:' + #typeCode")
    public List<ListItem> getDictByTypeCode(String typeCode) {
        return doGetDictByTypeCode(typeCode);
    }

    @Override
    @CachePut(value = "dictCache", key = "'fjec:dict:' + #typeCode")
    public List<ListItem> cacheDictByTypeCode(String typeCode) {
        return doGetDictByTypeCode(typeCode);
    }

    /**
     * 根据TypeCode获取字典表数据
     *
     * @param typeCode
     * @return
     */
    public List<ListItem> doGetDictByTypeCode(String typeCode) {
        QueryWrapper<SysDict> wrapper = new QueryWrapper<>();
        wrapper.eq("dictTypeCode", typeCode);
        wrapper.eq("valid", 1);
        wrapper.orderBy(true, true, "sort");
        List<SysDict> entityList = mapper.selectList(wrapper);
        List<ListItem> resultList = new ArrayList<>();
        for (SysDict entity : entityList) {
            ListItem item = new ListItem();
            item.setId(entity.getId());
            item.setKey(entity.getDictValue());
            item.setText(entity.getDisplayText());
            item.setValue(entity.getDictValue());
            item.setSort(entity.getSort());
            resultList.add(item);
        }
        return resultList;
    }

    @Override
    @Cacheable(value = "dictCache", key = "'fjec:dict:pcd'")
    public Map<String, Object> getPCD() {
        return doGetPCD();
    }

    @Override
    @CachePut(value = "dictCache", key = "'fjec:dict:pcd'")
    public Map<String, Object> cachePCD() {
        return doGetPCD();
    }

    /**
     * 获取省市区，Json格式
     *
     * @return
     */
    public Map<String, Object> doGetPCD() {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("province_list", this.getDictForKeyValue("PROVINCE"));
        resultMap.put("city_list", this.getDictForKeyValue("CITY"));
        resultMap.put("county_list", this.getDictForKeyValue("DISTRICT"));
        return resultMap;
    }

    /**
     * 根据typeCode获取KeyValue
     *
     * @param typeCode
     * @return
     */
    private Map<String, String> getDictForKeyValue(String typeCode) {
        QueryWrapper<SysDict> wrapper = new QueryWrapper<>();
        wrapper.eq("dictTypeCode", typeCode);
        wrapper.eq("valid", 1);
        wrapper.orderBy(true, true, "sort");
        List<SysDict> entityList = mapper.selectList(wrapper);
        Map<String, String> resultMap = new HashMap<>();
        for (SysDict entity : entityList) {
            resultMap.put(entity.getDictValue(), entity.getDisplayText());
        }
        return resultMap;
    }

    @Override
    public List<ListItem> test(Page page, String typeCode) {
        QueryWrapper<SysDict> query = new QueryWrapper<>();
        query.eq("dictTypeCode", typeCode);
        Page<SysDict> sysDictPage = mapper.selectPage(page, query);
        List<ListItem> resultList = new ArrayList<>();
        for (SysDict entity : sysDictPage.getRecords()) {
            ListItem item = new ListItem();
            item.setId(entity.getId());
            item.setKey(entity.getDictValue());
            item.setText(entity.getDisplayText());
            item.setValue(entity.getDictValue());
            item.setSort(entity.getSort());
            resultList.add(item);
        }
        return resultList;
    }

    //    @Override
    private void insertPcd() throws Exception {
        byte[] bytes = FileUtil.getInputStreamBytes(new FileInputStream("E:\\data\\desktop\\sql\\pcd.json"));
        String content = new String(bytes, "utf-8");
        Map<String, Object> map = JsonUtil.jsonToMap(content);
        Map<String, String> tmpMap = MapUtils.getMap(map, "province_list");
        // prov
        int i = 0;
        for (String key : tmpMap.keySet()) {
            System.out.println(key + ":" + tmpMap.get(key));
            SysDict sysDictProv = new SysDict();
            sysDictProv.setId(idGenerator.nextId());
            sysDictProv.setDictTypeCode("PROVINCE");
            sysDictProv.setDictValue(key);
            sysDictProv.setDisplayText(tmpMap.get(key));
            sysDictProv.setLeaf(0);
            sysDictProv.setSort(i);
            sysDictProv.setCreateTime(DateUtil.nowLdt());
            mapper.insert(sysDictProv);
            i++;
        }
        // city
        tmpMap = MapUtils.getMap(map, "city_list");
        i = 0;
        for (String key : tmpMap.keySet()) {
            System.out.println(key + ":" + tmpMap.get(key));
            SysDict sysDictCity = new SysDict();
            sysDictCity.setId(idGenerator.nextId());
            sysDictCity.setDictTypeCode("CITY");
            sysDictCity.setDictValue(key);
            sysDictCity.setDisplayText(tmpMap.get(key));
            sysDictCity.setLeaf(0);
            sysDictCity.setSort(i);
            sysDictCity.setCreateTime(DateUtil.nowLdt());
            mapper.insert(sysDictCity);
            i++;
        }
        // county
        tmpMap = MapUtils.getMap(map, "county_list");
        i = 0;
        for (String key : tmpMap.keySet()) {
            System.out.println(key + ":" + tmpMap.get(key));
            SysDict sysDictDist = new SysDict();
            sysDictDist.setId(idGenerator.nextId());
            sysDictDist.setDictTypeCode("DISTRICT");
            sysDictDist.setDictValue(key);
            sysDictDist.setDisplayText(tmpMap.get(key));
            sysDictDist.setLeaf(0);
            sysDictDist.setSort(i);
            sysDictDist.setCreateTime(DateUtil.nowLdt());
            mapper.insert(sysDictDist);
            i++;
        }
    }

}
